package com.zyy.smartcity1129.all;

import android.graphics.Color;
import android.view.View;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.bus.BusFragment;
import com.zyy.smartcity1129.citysubway.CitySubwayFragment;
import com.zyy.smartcity1129.data.all;
import com.zyy.smartcity1129.hd.HDFragment;
import com.zyy.smartcity1129.hospital.HospitalFragment;
import com.zyy.smartcity1129.house.HouseFragment;
import com.zyy.smartcity1129.lifepay.LifePayFragment;
import com.zyy.smartcity1129.movecar.MoveCarFragment;
import com.zyy.smartcity1129.movecar.MoveRecordFragment;
import com.zyy.smartcity1129.movie.MovieFragment;
import com.zyy.smartcity1129.parking.ParkingFragment;
import com.zyy.smartcity1129.reservation.ReservationFragment;
import com.zyy.smartcity1129.takeout.TakeoutFragment;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;
import com.zyy.smartcity1129.violation.ViolationFragment;
import com.zyy.smartcity1129.work.WorkFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class AllFragment extends BaseFragment {
    private RecyclerView rv1;
    private RecyclerView rv2;
    private SearchView search;
    int typt;
    private List<all.RowsBean> alllist;


    @Override
    protected int layId() {
        return R.layout.f_all;
    }

    @Override
    protected String title() {
        return "全部服务";
    }

    @Override
    protected boolean back() {
        return false;
    }

    @Override
    protected Boolean bottom() {
        return true;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        th(() -> {
            alllist = Http.getInstance().get("/prod-api/api/service/list", null, all.class, 1).getRows();
            List<String> list=new ArrayList<>(Arrays.asList("车主服务","生活服务","便民服务"));
            List<all.RowsBean> list1=new ArrayList<>();
            list1.addAll(alllist.stream().filter(a->a.getServiceType().equals(list.get(0))).collect(Collectors.toList()));
            all.RowsBean rowsBean = new all.RowsBean();
            rowsBean.setServiceName("预约检车");
            rowsBean.setServiceType("车主服务");
            list1.add(rowsBean);
            all.RowsBean rowsBean1 = new all.RowsBean();
            rowsBean1.setServiceName("移车挪车");
            rowsBean1.setServiceType("车主服务");
            list1.add(rowsBean1);
            all.RowsBean rowsBean2 = new all.RowsBean();
            rowsBean2.setServiceName("违章查询");
            rowsBean2.setServiceType("车主服务");
            list1.add(rowsBean2);

            typt=0;
            RvAd<all.RowsBean> ad2 = new RvAd<all.RowsBean>(list1, R.layout.r_all) {
                private void initView(View vi) {
                    image = (ImageView) vi.findViewById(R.id.image);
                    tv1 = (TextView) vi.findViewById(R.id.tv1);
                }

                private TextView tv1;
                private ImageView image;

                @Override
                protected void initR(View vi, all.RowsBean rowsBean, int p) {
                    initView(vi);
                    tv1.setText(rowsBean.getServiceName());
                    Glide.with(image.getContext()).load(Http.base + rowsBean.getImgUrl()).into(image);
                    vi.setOnClickListener(v->{
                        adds(rowsBean.getServiceName());
                    });
                }
            };

            RvAd<String> ad1=new RvAd<String>(list,R.layout.r_text) {
                @Override
                protected void initR(View vi, String s, int p) {
                    TextView tv=vi.findViewById(R.id.tv);
                    tv.setText(s);
                    if (typt == p)
                        tv.setBackgroundColor(Color.parseColor("#E0E0E0"));
                    else
                        tv.setBackgroundColor(Color.WHITE);
                    vi.setOnClickListener(v->{
                        typt=p;
                        notifyDataSetChanged();
                        list1.clear();
                        list1.addAll(alllist.stream().filter(a->a.getServiceType().equals(list.get(p))).collect(Collectors.toList()));
                        ad2.notifyDataSetChanged();
                    });
                }
            };
            search.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
                @Override
                public boolean onQueryTextSubmit(String s) {
                    list1.clear();
                    list1.addAll(alllist.stream().filter(a->a.getServiceName().contains(s)).collect(Collectors.toList()));
                    if (list1.size()!=0){
                        ad2.notifyDataSetChanged();
                    }else {
                        Toast.makeText(getContext(), "没有查询到结果", Toast.LENGTH_SHORT).show();
                    }
                    return true;
                }

                @Override
                public boolean onQueryTextChange(String s) {
                    return false;
                }
            });
            ui(() -> {
                rv1.setAdapter(ad1);
                rv1.setLayoutManager(new LinearLayoutManager(getContext()));
                rv2.setAdapter(ad2);
                rv2.setLayoutManager(new GridLayoutManager(getContext(),3));
            });
        });
    }

    private void initView(View iv) {
        rv1 = (RecyclerView) iv.findViewById(R.id.rv1);
        search = (SearchView) iv.findViewById(R.id.search);
        rv2 = (RecyclerView) iv.findViewById(R.id.rv2);
    }

    public void adds(String name){
        switch (name){
            case "外卖订餐":
                add(new TakeoutFragment());
                break;
            case "生活缴费":
                add(new LifePayFragment());
                break;
            case "违章查询":
                add(new ViolationFragment());
                break;
            case "找工作":
                add(new WorkFragment());
                break;
            case "智慧巴士":
                add(new BusFragment());
                break;
            case "移车挪车":
                add(new MoveCarFragment());
                break;
            case "停哪儿":
                add(new ParkingFragment());
                break;
            case "城市地铁":
                add(new CitySubwayFragment());
                break;
            case "找房子":
                add(new HouseFragment());
                break;
            case "门诊预约":
                add(new HospitalFragment());
                break;
            case "看电影":
                add(new MovieFragment());
                break;
            case "预约检车":
                add(new ReservationFragment());
                break;
            case "活动管理":
                add(new HDFragment());
                break;
        }
    }
}